// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <nebula/types/meta/fixed_size_binary_type.h>

namespace nebula {

    constexpr Type::type FixedSizeBinaryType::type_id;

    std::string FixedSizeBinaryType::compute_fingerprint() const {
        std::stringstream ss;
        ss << type_id_fingerprint(*this) << "[" << byte_width_ << "]";
        return ss.str();
    }

    int FixedSizeBinaryType::bit_width() const { return CHAR_BIT * byte_width(); }

    turbo::Result<std::shared_ptr<DataType>> FixedSizeBinaryType::create(int32_t byte_width) {
        if (byte_width < 0) {
            return turbo::invalid_argument_error("Negative FixedSizeBinaryType byte width");
        }
        if (byte_width > std::numeric_limits<int>::max() / CHAR_BIT) {
            // bit_width() would overflow
            return turbo::invalid_argument_error("byte width of FixedSizeBinaryType too large");
        }
        return std::make_shared<FixedSizeBinaryType>(byte_width);
    }

    std::string FixedSizeBinaryType::to_string(bool show_metadata) const {
        std::stringstream ss;
        ss << "fixed_size_binary[" << byte_width_ << "]";
        return ss.str();
    }

}  // namespace nebula
